package com.xhg.model;

import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.xhg.common.PageNavation;

/**
 * y_user model.
将表结构放在此，消除记忆负担
mysql> desc y_user;
-----------------------------------------------------------------------------------
字段名			Id		userName	password	identity	state	createTime
Comment		id		用户名			密码 			标记位			状态		创建时间
类型			bigint	varchar		varchar     int			int		timestamp
Null		NO		NO			NO			NO			NO		NO
默认值											1			1		CURRENT_TIMESTAMP
-----------------------------------------------------------------------------------
数据库字段名建议使用驼峰命名规则，便于与 java 代码保持一致，如字段名： userId
 */
public class User extends Model<User> implements BaseModelImpl<User,Long>{
	
	private static final long serialVersionUID = 888093912149520765L;
	private static String sql;
	public static final User dao = new User();
	
	@Override
	public boolean save(User obj) {
		return obj.save();
	}
	@Override
	public boolean update(User obj) {
		return obj.update();
	}
	@Override
	public User queryById(Long id) {
		return dao.findById(id);
	}
	@Override
	public boolean deleteByIds(String ids) {
		String sql = "update y_user SET state=0 WHERE id in("+ids+")";
		return Db.update(sql)>0;
	}
	@Override
	public Page<User> queryList(PageNavation page) {
		Page<User> userPage=dao.paginate(page.getPageNumber(),page.getPageSize(),
				"select *","from y_user where identity=1 AND state=1 order by id desc");
		return userPage;
	}
	/**
	 * 登陆action
	 * @param user
	 * @return
	 */
	public User Login(User user) {
		sql="select * from y_user where userName=? AND password=?";
		return dao.findFirst(sql,user.getStr("userName"),user.getStr("password"));
	}
    /**
     * 验证用户名是否存在
     */
    public Boolean checkUserName(Map<String,Object> paramMap) {
    	sql="select Count(id) from y_user where state=1 AND userName ='"+paramMap.get("userName")+"'";
    	if(paramMap.get("id")!=null){
    		sql+=" AND id!="+Integer.parseInt(paramMap.get("id").toString());
    	}
    	Long count=Db.queryLong(sql);
    	return count>0;
    }
}
